package benny_driving_ser.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import benny_driving_ser.dao.AppVersionDao;
import benny_driving_ser.entity.AppVersion;
import benny_driving_ser.util.DBUtil;

public class AppVersionDaoImpl implements AppVersionDao {
	private AppVersionDaoImpl() {

	}

	private static class AppVersionDaoImplHolder {
		private static AppVersionDaoImpl instance = new AppVersionDaoImpl();
	}

	public static AppVersionDaoImpl getInstance() {
		return AppVersionDaoImplHolder.instance;
	}

	public AppVersion getDriVersion() {
		String sql = "SELECT dri_version,dri_app_url FROM app_version";
		Connection con = DBUtil.getInstance().openConnection();
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery(sql);
			if (rs.next()) {
				AppVersion av = new AppVersion(rs.getString(1) + "",
						rs.getString(2) + "");
				return av;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.getInstance().closeConnection(con);
		}
		return new AppVersion("null", "null");
	}

	public AppVersion getUserVersion() {
		String sql = "SELECT user_version,user_app_url FROM app_version";
		Connection con = DBUtil.getInstance().openConnection();
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery(sql);
			if (rs.next()) {
				AppVersion av = new AppVersion(rs.getString(1) + "",
						rs.getString(2) + "");
				DBUtil.getInstance().closeConnection(con);
				return av;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.getInstance().closeConnection(con);
		return new AppVersion("null", "null");
	}

	public AppVersion getDriVersionIOS() {
		String sql = "SELECT dri_version_ios,dri_app_url_ios FROM app_version";
		Connection con = DBUtil.getInstance().openConnection();
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery(sql);
			if (rs.next()) {
				AppVersion av = new AppVersion(rs.getString(1) + "",
						rs.getString(2) + "");
				return av;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.getInstance().closeConnection(con);
		}
		return new AppVersion("null", "null");
	}
	
	public AppVersion getUserVersionIOS() {
		String sql = "SELECT user_version_ios,user_app_url_ios FROM app_version";
		Connection con = DBUtil.getInstance().openConnection();
		try {
			con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
			Statement s = con.createStatement();
			ResultSet rs = s.executeQuery(sql);
			if (rs.next()) {
				AppVersion av = new AppVersion(rs.getString(1) + "",
						rs.getString(2) + "");
				DBUtil.getInstance().closeConnection(con);
				return av;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.getInstance().closeConnection(con);
		return new AppVersion("null", "null");
	}
}
